/*
  In App.xaml:
  <Application.Resources>
      <vm:ViewModelLocator xmlns:vm="clr-namespace:JiveMessenger"
                           x:Key="Locator" />
  </Application.Resources>
  
  In the View:
  DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"

  You can also use Blend to do all this with the tool's support.
  See http://www.galasoft.ch/mvvm
*/

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Views;
using JiveMessenger.SampleData;
using JiveMessenger.User;
using JiveMessenger.Views;
using Microsoft.Practices.ServiceLocation;
using System;
using System.ComponentModel;
using System.Windows;
using Windows.UI.Xaml;

namespace JiveMessenger.ViewModels
{
    /// <summary>
    /// This class contains static references to all the view models in the
    /// application and provides an entry point for the bindings.
    /// </summary>
    public class ViewModelLocator : INotifyPropertyChanged
    {
        public NavigationService NavService = new NavigationService();


        private string _viewModelAddGroupKey = System.Guid.NewGuid().ToString();
        public string ViewModelAddGroupKey
        {
            get { return _viewModelAddGroupKey; }
            set
            {
                if (value != _viewModelAddGroupKey)
                {
                    _viewModelAddGroupKey = value;
                    RaisePropertyChanged("ViewModelAddGroupKey");
                }
            }
        }

        private string _viewModelAddEmoticonKey = System.Guid.NewGuid().ToString();
        public string ViewModelAddEmoticonKey
        {
            get { return _viewModelAddEmoticonKey; }
            set
            {
                if (value != _viewModelAddEmoticonKey)
                {
                    _viewModelAddEmoticonKey = value;
                    RaisePropertyChanged("ViewModelAddEmoticonKey");
                }
            }
        }



        private string _viewModelMainWindowKey = System.Guid.NewGuid().ToString();
        public string ViewModelMainWindowKey
        {
            get { return _viewModelMainWindowKey; }
            set
            {
                if (value != _viewModelMainWindowKey)
                {
                    _viewModelMainWindowKey = value;
                    RaisePropertyChanged("ViewModelMainWindow");
                }
            }
        }

        private string _viewModelContactListKey = System.Guid.NewGuid().ToString();
        public string ViewModelContactListKey
        {
            get { return _viewModelContactListKey; }
            set
            {
                if (value != _viewModelContactListKey)
                {
                    _viewModelContactListKey = value;
                    RaisePropertyChanged("ViewModelContactList");
                }
            }
        }

        private string _viewModelLoginControlKey = System.Guid.NewGuid().ToString();
        public string ViewModelLoginControlKey
        {
            get { return _viewModelLoginControlKey; }
            set
            {
                if (value != _viewModelLoginControlKey)
                {
                    _viewModelLoginControlKey = value;
                    RaisePropertyChanged("ViewModelLoginControlKey");
                }
            }
        }

        private string _currentClientKey = System.Guid.NewGuid().ToString();
        public string CurrentClientKey
        {
            get { return _currentClientKey; }
            set
            {
                if (value != _currentClientKey)
                {
                    _currentClientKey = value;
                    RaisePropertyChanged("CurrentClient");
                }
            }
        }

        private static string _viewModelNotificationsKey = System.Guid.NewGuid().ToString();
        public string ViewModelNotificationsKey
        {
            get { return _viewModelNotificationsKey; }
            set
            {
                if (value != _viewModelNotificationsKey)
                {
                    _viewModelNotificationsKey = value;
                    RaisePropertyChanged("ViewModelNotificationsKey");
                }
            }
        }

        private string _viewModelRegisterWindowKey = System.Guid.NewGuid().ToString();
        public string ViewModelRegisterWindowKey
        {
            get { return _viewModelRegisterWindowKey; }
            set
            {
                if (value != _viewModelRegisterWindowKey)
                {
                    _viewModelRegisterWindowKey = value;
                    RaisePropertyChanged("ViewModelRegisterWindowKey");
                }
            }
        }

        private string _viewModelResetPasswordKey = System.Guid.NewGuid().ToString();
        public string ViewModelResetPasswordKey
        {
            get { return _viewModelResetPasswordKey; }
            set
            {
                if (value != _viewModelResetPasswordKey)
                {
                    _viewModelResetPasswordKey = value;
                    RaisePropertyChanged("ViewModelResetPasswordKey");
                }
            }
        }
        private string _viewModelMessageDownloadKey = System.Guid.NewGuid().ToString();
        public string ViewModelMessageDownloadKey
        {
            get { return _viewModelMessageDownloadKey; }
            set
            {
                if (_viewModelMessageDownloadKey != value)
                {
                    _viewModelMessageDownloadKey = value;
                    RaisePropertyChanged("ViewModelMessageDownloadKey");
                }
            }
        }
        private string _viewModelSettingsPageKey = System.Guid.NewGuid().ToString();
        public string ViewModelSettingsPageKey
        {
            get { return _viewModelSettingsPageKey; }
            set
            {
                if (_viewModelSettingsPageKey != value)
                {
                    _viewModelSettingsPageKey = value;
                    RaisePropertyChanged("ViewModelSettingsPageKey");
                }
            }
        }
        private string _viewModelCropAvatarPageKey = System.Guid.NewGuid().ToString();
        public string ViewModelCropAvatarPageKey
        {
            get { return _viewModelCropAvatarPageKey; }
            set
            {
                if (_viewModelCropAvatarPageKey != value)
                {
                    _viewModelCropAvatarPageKey = value;
                    RaisePropertyChanged("ViewModelCropAvatarPageKey");
                }
            }
        }

        private string _viewModelAddContactKey = System.Guid.NewGuid().ToString();
        public string ViewModelAddContactKey
        {
            get { return _viewModelAddContactKey; }
            set
            {
                if (_viewModelAddContactKey != value)
                {
                    _viewModelAddContactKey = value;
                    RaisePropertyChanged("ViewModelAddContactKey");
                }
            }
        }
        private string _viewModelCropEmoticonPageKey = System.Guid.NewGuid().ToString();
        public string ViewModelCropEmoticonPageKey
        {
            get { return _viewModelCropEmoticonPageKey; }
            set
            {
                if (_viewModelCropEmoticonPageKey != value)
                {
                    _viewModelCropEmoticonPageKey = value;
                    RaisePropertyChanged("ViewModelCropEmoticonPageKey");
                }
            }
        }

        /// <summary>
        /// Initializes a new instance of the ViewModelLocator class.
        /// </summary>
        public ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

            if (ViewModelBaseEx.IsInDesignModeStatic)
            {
                // Create design time view services and models
                if (!SimpleIoc.Default.IsRegistered<IClient>())
                    SimpleIoc.Default.Register<IClient>(() => new SampleClient());

                if (!SimpleIoc.Default.IsRegistered<IViewModelNotifications>())
                    SimpleIoc.Default.Register<IViewModelNotifications>(() => new SampleViewModelNotifications());

                if (!SimpleIoc.Default.IsRegistered<IViewModelContactList>())
                    SimpleIoc.Default.Register<IViewModelContactList>(() => new SampleViewModelContactList());

                if (!SimpleIoc.Default.IsRegistered<IViewModelMainPage>())
                    SimpleIoc.Default.Register<IViewModelMainPage>(() => new SampleViewModelMainPage(CurrentClient));

                if (!SimpleIoc.Default.IsRegistered<ViewModelAddEmoticon>())
                    SimpleIoc.Default.Register<ViewModelAddEmoticon>(() => new ViewModelAddEmoticon(CurrentClient, NavService));

                if (!SimpleIoc.Default.IsRegistered<IViewModelLoginControl>())
                    SimpleIoc.Default.Register<IViewModelLoginControl>(() => new ViewModelLoginControl(CurrentClient));

                //if (!SimpleIoc.Default.IsRegistered<IViewModelContactList>())
                //    SimpleIoc.Default.Register<IViewModelContactList>(() => new ViewModelContactList(CurrentClient));

                if (!SimpleIoc.Default.IsRegistered<ViewModelAddGroup>())
                    SimpleIoc.Default.Register<ViewModelAddGroup>(() => new ViewModelAddGroup(CurrentClient,NavService));

                if (!SimpleIoc.Default.IsRegistered<ViewModelRegisterUser>())
                    SimpleIoc.Default.Register<ViewModelRegisterUser>(() => new ViewModelRegisterUser(CurrentClient));

                if (!SimpleIoc.Default.IsRegistered<ViewModelResetPasswordPage>())
                    SimpleIoc.Default.Register<ViewModelResetPasswordPage>(() => new ViewModelResetPasswordPage(CurrentClient));

                if (!SimpleIoc.Default.IsRegistered<ViewModelSettingsPage>())
                    SimpleIoc.Default.Register<ViewModelSettingsPage>(() => new ViewModelSettingsPage(CurrentClient, NavService));

                if (!SimpleIoc.Default.IsRegistered<ViewModelMessageDownloadPage>())
                    SimpleIoc.Default.Register<ViewModelMessageDownloadPage>(() => new ViewModelMessageDownloadPage(CurrentClient, NavService));

                if (!SimpleIoc.Default.IsRegistered<ViewModelCropAvatarPage>())
                    SimpleIoc.Default.Register<ViewModelCropAvatarPage>(() => new ViewModelCropAvatarPage(CurrentClient, NavService));

                if (!SimpleIoc.Default.IsRegistered<ViewModelCropEmoticonPage>())
                    SimpleIoc.Default.Register<ViewModelCropEmoticonPage>(() => new ViewModelCropEmoticonPage(CurrentClient, NavService));

                if (!SimpleIoc.Default.IsRegistered<ViewModelAddContact>())
                    SimpleIoc.Default.Register<ViewModelAddContact>(() => new ViewModelAddContact(CurrentClient, NavService));

                if (!SimpleIoc.Default.IsRegistered<IViewModelManageGroup>())
                    SimpleIoc.Default.Register<IViewModelManageGroup>(() => new SampleViewModelManageGroup(),true);
            }
            else
            {

                // Create run time view services and models

                if (!SimpleIoc.Default.IsRegistered<IViewModelMainPage>())
                    SimpleIoc.Default.Register<IViewModelMainPage>(() => new ViewModelMainPage(CurrentClient, NavService));

                if (!SimpleIoc.Default.IsRegistered<ViewModelAddEmoticon>())
                    SimpleIoc.Default.Register<ViewModelAddEmoticon>(() => new ViewModelAddEmoticon(CurrentClient,NavService));

                if (!SimpleIoc.Default.IsRegistered<IClient>())
                    SimpleIoc.Default.Register<IClient>(() => new Client());

                if (!SimpleIoc.Default.IsRegistered<IViewModelNotifications>())
                    SimpleIoc.Default.Register<IViewModelNotifications>(() => new ViewModelNotifications(CurrentClient));

                if (!SimpleIoc.Default.IsRegistered<IViewModelContactList>())
                    SimpleIoc.Default.Register<IViewModelContactList>(() => new ViewModelContactList(CurrentClient, NavService));

                if (!SimpleIoc.Default.IsRegistered<IViewModelLoginControl>())
                    SimpleIoc.Default.Register<IViewModelLoginControl>(() => new ViewModelLoginControl(CurrentClient));

                if (!SimpleIoc.Default.IsRegistered<ViewModelAddGroup>())
                    SimpleIoc.Default.Register<ViewModelAddGroup>(() => new ViewModelAddGroup(CurrentClient, NavService));

                if (!SimpleIoc.Default.IsRegistered<ViewModelRegisterUser>())
                    SimpleIoc.Default.Register<ViewModelRegisterUser>(() => new ViewModelRegisterUser(CurrentClient));

                if (!SimpleIoc.Default.IsRegistered<ViewModelResetPasswordPage>())
                    SimpleIoc.Default.Register<ViewModelResetPasswordPage>(() => new ViewModelResetPasswordPage(CurrentClient));

                if (!SimpleIoc.Default.IsRegistered<ViewModelSettingsPage>())
                    SimpleIoc.Default.Register<ViewModelSettingsPage>(() => new ViewModelSettingsPage(CurrentClient, NavService));

                if (!SimpleIoc.Default.IsRegistered<ViewModelMessageDownloadPage>())
                    SimpleIoc.Default.Register<ViewModelMessageDownloadPage>(() => new ViewModelMessageDownloadPage(CurrentClient, NavService));

                if (!SimpleIoc.Default.IsRegistered<ViewModelCropAvatarPage>())
                    SimpleIoc.Default.Register<ViewModelCropAvatarPage>(() => new ViewModelCropAvatarPage(CurrentClient, NavService));

                if (!SimpleIoc.Default.IsRegistered<ViewModelCropEmoticonPage>())
                    SimpleIoc.Default.Register<ViewModelCropEmoticonPage>(() => new ViewModelCropEmoticonPage(CurrentClient, NavService));

                if (!SimpleIoc.Default.IsRegistered<ViewModelAddContact>())
                    SimpleIoc.Default.Register<ViewModelAddContact>(() => new ViewModelAddContact(CurrentClient, NavService));

                //if (!SimpleIoc.Default.IsRegistered<IViewModelManageGroup>())
                //    SimpleIoc.Default.Register<IViewModelManageGroup>(() => new SampleViewModelManageGroup());
            }
        }

        public ViewModelAddContact ViewModelAddContact
        {
            get
            {
                return ServiceLocator.Current.GetInstance<ViewModelAddContact>(ViewModelAddContactKey);
            }
        }
        public ViewModelCropAvatarPage ViewModelCropAvatarPage
        {
            get
            {
                return ServiceLocator.Current.GetInstance<ViewModelCropAvatarPage>(ViewModelCropAvatarPageKey);
            }
        }
        public ViewModelCropEmoticonPage ViewModelCropEmoticonPage
        {
            get
            {
                return ServiceLocator.Current.GetInstance<ViewModelCropEmoticonPage>(ViewModelCropEmoticonPageKey);
            }
        }
        public ViewModelMessageDownloadPage ViewModelMessageDownloadPage
        {
            get
            {
                return ServiceLocator.Current.GetInstance<ViewModelMessageDownloadPage>(ViewModelMessageDownloadKey);
            }
        }

        public ViewModelSettingsPage ViewModelSettingsPage
        {
            get
            {
                return ServiceLocator.Current.GetInstance<ViewModelSettingsPage>(ViewModelSettingsPageKey);
            }
        }

        public IViewModelManageGroup SampleViewModelManageGroup
        {
            get
            {
                return ServiceLocator.Current.GetInstance<IViewModelManageGroup>();
            }
        }

        public ViewModelResetPasswordPage ViewModelResetPasswordWindow
        {
            get
            {
                return ServiceLocator.Current.GetInstance<ViewModelResetPasswordPage>(ViewModelResetPasswordKey);
            }
        }

        public ViewModelRegisterUser ViewModelRegisterPage
        {
            get
            {
                return ServiceLocator.Current.GetInstance<ViewModelRegisterUser>(ViewModelRegisterWindowKey);
            }
        }

        public IViewModelNotifications ViewModelNotifications
        {
            get
            {
                return ServiceLocator.Current.GetInstance<IViewModelNotifications>(ViewModelNotificationsKey);
            }
        }

        public ViewModelAddGroup ViewModelAddGroup
        {
            get
            {
                return ServiceLocator.Current.GetInstance<ViewModelAddGroup>(ViewModelAddGroupKey);
            }
        }

        public IViewModelContactList ViewModelContactList
        {
            get
            {
                return ServiceLocator.Current.GetInstance<IViewModelContactList>(ViewModelContactListKey);
            }
        }

        public IViewModelLoginControl ViewModelLoginControl
        {
            get
            {
                return ServiceLocator.Current.GetInstance<IViewModelLoginControl>(ViewModelLoginControlKey);
            }
        }


        public ViewModelAddEmoticon ViewModelAddEmoticon
        {
            get
            {
                return ServiceLocator.Current.GetInstance<ViewModelAddEmoticon>(ViewModelAddEmoticonKey);
            }
        }
        public IClient CurrentClient
        {
            get
            {
                return ServiceLocator.Current.GetInstance<IClient>(CurrentClientKey);
            }
        }
        public IViewModelMainPage ViewModelMainPage
        {
            get
            {
                return ServiceLocator.Current.GetInstance<IViewModelMainPage>(ViewModelMainWindowKey);
            }
        }

        //public static void Cleanup()
        //{
        //    // CleanupCurrentClient();
        //    // CleanupViewModelAddEmoticons();
        //    // CleanupViewModelGetUserEmoticons();
        //    // CleanupViewModelOpenLogs();
        //    // CleanupViewModelMainWindow();
        //    // CleanupViewModelContactList();



        //    // Messenger.Reset();
        //}

        //public static void CleanupCurrentClient()
        //{
        //    var viewModelLocator = VmHelper.Locator;
        //    viewModelLocator.CurrentClient.Cleanup();

        //}

        //public static void CleanupViewModelContactList()
        //{
        //    var viewModelLocator = VmHelper.Locator;
        //    viewModelLocator.ViewModelContactList.Cleanup();
        //}

        //public static void CleanupViewModelMainWindow()
        //{
        //    var viewModelLocator = VmHelper.Locator;
        //    viewModelLocator.ViewModelMainPage.Cleanup();
        //}

        //public static void CleanupViewModelGetUserEmoticons()
        //{
        //    var viewModelLocator = VmHelper.Locator;
        //    viewModelLocator.ViewModelGetUserEmoticons.Cleanup();
        //}

        //public static void CleanupViewModelAddEmoticons()
        //{
        //    var viewModelLocator = VmHelper.Locator;
        //    viewModelLocator.ViewModelAddEmoticon.Cleanup();
        //}

        protected virtual void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}